package br.com.salao.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

public class Internacionalizacao implements Serializable {
	
	/**
	 * 
	 */
	
	Logger logger = Logger.getLogger(Internacionalizacao.class);
	private static final long serialVersionUID = 1L;
	private List<SelectItem> availableLocales;
	private Locale currentLocale;	
	private String selectLocale;
	int count = 0;
	
	static{
		
	}
	
	public Internacionalizacao(){		
		
		if(count ==0){
			setCurrentLocale(FacesContext.getCurrentInstance().getViewRoot().getLocale());
			selectLocale = currentLocale.getLanguage();	
			count++;
		}
		
		logger.info("Atual locale construtor:" + currentLocale.getDisplayName() +currentLocale.getLanguage());
/*		UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();  
		  currentLocale = Locale.US;  
		  viewRoot.setLocale(currentLocale); */
	}
	
	private void generateAvailableLocales() {
		availableLocales = new ArrayList<SelectItem>(0);
		
		// Add the default locale
		availableLocales.add
                (makeLocaleItem(FacesContext.getCurrentInstance().getApplication().getDefaultLocale()));
		
		// Add any other supported locales
		for (Iterator<Locale> iter =
			     FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
		     iter.hasNext();) {
			availableLocales.add(makeLocaleItem(iter.next()));
		}
	}
	
	private SelectItem makeLocaleItem(Locale toWrap) {
		if (toWrap != null) {
			return new SelectItem(toWrap.getLanguage(),
					       	      toWrap.getDisplayName());
		}
		
		return null;
	}
	public List<SelectItem> getAvailableLocales() {
		if (availableLocales == null) {
			generateAvailableLocales();
		}
		
		return availableLocales;
	}

	public void setAvailableLocales(List<SelectItem> availableLocales) {
		this.availableLocales = availableLocales;
	}

	public Locale getCurrentLocale() {
		return currentLocale;
	}

	public void setCurrentLocale(Locale currentLocale) {
		this.currentLocale = currentLocale;
	}
	public void applyLocale(Locale toApply) {
		System.out.println("Apply Locale: " + 
                                    toApply.getDisplayName() + " (" + toApply.getLanguage() + ")");
		
		setCurrentLocale(toApply);		
		FacesContext.getCurrentInstance().getViewRoot().setLocale(toApply);
		
/*		UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();  
		currentLocale = toApply;  
		viewRoot.setLocale(currentLocale); 		*/	
	}
	
	public void localeChanged(ValueChangeEvent event) {
		if (event.getNewValue() != null) {
			applyLocale(new Locale(event.getNewValue().toString()));
		}
	}

	public String getSelectLocale() {
		return selectLocale;
	}

	public void setSelectLocale(String selectLocale) {
		this.selectLocale = selectLocale;
	}
	

}
